package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.api.stock.CancelOrderApplyServiceAPI;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamCancelOrderBean;
import com.steel.yfmall.cloud.assistant.utils.kd.CosmicLoginUtil;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @program: CancelOrderApplyServiceAPIImpl
 * @author: liuzhijun
 * @date: 2021-04-05 11:02
 **/
@Service
@Slf4j
public class CancelOrderApplyServiceAPIImpl implements CancelOrderApplyServiceAPI {

    @Override
    public APIMsgBean<Boolean> cancelOrderApply(APIParamCancelOrderBean apiParamCancelOrderBean) {
        APIMsgBean<Boolean> booleanAPIMsgBean = new APIMsgBean<>();
        booleanAPIMsgBean.setData(false);
        booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean());
        if (apiParamCancelOrderBean == null) {
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("参数不能为空"));
            return booleanAPIMsgBean;
        }
        if (StringUtils.isBlank(apiParamCancelOrderBean.getMasterOrderSn())) {
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("销售订单号不能为空"));
            return booleanAPIMsgBean;
        }

        String result = "";
        String url = "";
        try {
            //获取登录token
            String accessToken = CosmicLoginUtil.getAccessToken(CosmicLoginUtil.GetAPPToken());
            url = DS_COSMIC_ORDERCANCEL_URL + "?access_token=" + accessToken;
            String str = "";
            JSONObject obj = new JSONObject();
            obj.put("yfgt_ecomordercode", apiParamCancelOrderBean.getMasterOrderSn());
            obj.put("org", "yf");
            str = obj.toString();
            result = CosmicLoginUtil.GetHttpMsg(str, url);
        } catch (Exception e) {
            log.info("请求url异常:" + url);
        }
        JSONObject jsonObject = JSON.parseObject(result);
        Boolean bool = (Boolean) jsonObject.get("success");
        if (bool) {
            booleanAPIMsgBean.setData(true);
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean());
        } else {
            booleanAPIMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(jsonObject.get("message").toString()));
        }
        return booleanAPIMsgBean;
    }


    @Value("${ds_cosmic_ordercancel_url}")
    private String DS_COSMIC_ORDERCANCEL_URL;
}
